import { Context } from 'koa';
import articleService from '../services/article.service';

/**
 * 获取文章列表
 * @param ctx Koa上下文
 */
export const getArticles = async (ctx: Context) => {
  try {
    const { 
      page, 
      limit, 
      status, 
      category_id, 
      tag_id, 
      author_id, 
      keyword, 
      sort_by, 
      sort_order, 
      is_pinned, 
      is_featured 
    } = ctx.query;

    const params = {
      page: page ? parseInt(page as string) : undefined,
      limit: limit ? parseInt(limit as string) : undefined,
      status: status as 'draft' | 'published' | 'archived',
      category_id: category_id as string,
      tag_id: tag_id as string,
      author_id: author_id as string,
      keyword: keyword as string,
      sort_by: sort_by as string,
      sort_order: sort_order as 'ASC' | 'DESC',
      is_pinned: is_pinned ? is_pinned === 'true' : undefined,
      is_featured: is_featured ? is_featured === 'true' : undefined,
    };

    const result = await articleService.getArticles(params);
    
    ctx.body = {
      code: 200,
      data: result,
      message: '获取文章列表成功',
    };
  } catch (error: any) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: `获取文章列表失败: ${error.message}`,
    };
  }
};

/**
 * 获取文章详情
 * @param ctx Koa上下文
 */
export const getArticleById = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    const { increment_view } = ctx.query;
    
    const incrementView = increment_view === 'true';
    const article = await articleService.getArticleById(id, incrementView);
    
    ctx.body = {
      code: 200,
      data: article,
      message: '获取文章详情成功',
    };
  } catch (error: any) {
    ctx.status = 404;
    ctx.body = {
      code: 404,
      message: `获取文章详情失败: ${error.message}`,
    };
  }
};

/**
 * 创建文章
 * @param ctx Koa上下文
 */
export const createArticle = async (ctx: Context) => {
  try {
    const {
      title,
      content,
      summary,
      slug,
      cover_image,
      status,
      category_id,
      tags,
      meta_title,
      meta_description,
      keywords,
      visibility,
      password,
      is_pinned,
      is_featured,
    } = ctx.request.body;
    
    // 验证必填字段
    if (!title || !content) {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: '标题和内容不能为空',
      };
      return;
    }
    
    // 从认证中获取用户ID
    const author_id = ctx.state.user.id;
    
    const article = await articleService.createArticle({
      title,
      content,
      summary,
      slug,
      cover_image,
      status,
      author_id,
      category_id,
      tags,
      meta_title,
      meta_description,
      keywords,
      visibility,
      password,
      is_pinned,
      is_featured,
    });
    
    ctx.status = 201;
    ctx.body = {
      code: 201,
      data: article,
      message: '创建文章成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `创建文章失败: ${error.message}`,
    };
  }
};

/**
 * 更新文章
 * @param ctx Koa上下文
 */
export const updateArticle = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    const {
      title,
      content,
      summary,
      slug,
      cover_image,
      status,
      category_id,
      tags,
      meta_title,
      meta_description,
      keywords,
      visibility,
      password,
      is_pinned,
      is_featured,
    } = ctx.request.body;
    
    const article = await articleService.updateArticle(id, {
      title,
      content,
      summary,
      slug,
      cover_image,
      status,
      category_id,
      tags,
      meta_title,
      meta_description,
      keywords,
      visibility,
      password,
      is_pinned,
      is_featured,
    });
    
    ctx.body = {
      code: 200,
      data: article,
      message: '更新文章成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `更新文章失败: ${error.message}`,
    };
  }
};

/**
 * 删除文章
 * @param ctx Koa上下文
 */
export const deleteArticle = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    
    const result = await articleService.deleteArticle(id);
    
    ctx.body = {
      code: 200,
      data: result,
      message: '删除文章成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `删除文章失败: ${error.message}`,
    };
  }
};

/**
 * 更改文章状态
 * @param ctx Koa上下文
 */
export const changeArticleStatus = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    const { status } = ctx.request.body;
    
    if (!['draft', 'published', 'archived'].includes(status)) {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: '无效的文章状态',
      };
      return;
    }
    
    const article = await articleService.changeArticleStatus(id, status);
    
    ctx.body = {
      code: 200,
      data: article,
      message: '更改文章状态成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `更改文章状态失败: ${error.message}`,
    };
  }
}; 